package servlet;

import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import bean.GradeDesign;
import dao.StudentSql;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

public class UploadServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        response.setContentType("text/html;charset=utf-8");
        //1.创建工厂类DiskFileItemFactory对象
        DiskFileItemFactory factory = new DiskFileItemFactory();
        //2.使用工厂创建解析器对象
        ServletFileUpload fileUpload = new ServletFileUpload(factory);
        GradeDesign gradeDesign = new GradeDesign();
        String ID = null,Name = null,Depart = null,Major = null,Title = null,Phone = null,RWS = null,KTBG = null,BYLW = null,CODE =null;
        //3.使用解析器来解析request对象
        try {
            // 使用解析器对象解析request，得到FileItem列表
            List<FileItem> list = fileUpload.parseRequest(request);
            // 遍历所有表单项
            for(FileItem fileItem : list) {
                // 如果当前表单项为普通表单项
                if (fileItem.isFormField()) {
                    switch (fileItem.getFieldName()) {
                        case "ID": ID = fileItem.getString("utf-8");
                            gradeDesign.setID(ID);
                            break;
                        case "Name": Name = fileItem.getString("utf-8");
                            gradeDesign.setName(Name);
                            break;
                        case "Depart": Depart = fileItem.getString("utf-8");
                            gradeDesign.setDepart(Depart);
                            break;
                        case "Major": Major = fileItem.getString("utf-8");
                            gradeDesign.setMajor(Major);
                            break;
                        case "Title": Title = fileItem.getString("utf-8");
                            gradeDesign.setTitle(Title);
                            break;
                        case "Phone": Phone = fileItem.getString("utf-8");
                            gradeDesign.setPhone(Phone);
                            break;
                        default:
                            break;
                    }
                } else {//如果当前表单项不是普通表单项，说明就是文件字段
                    String name = fileItem.getName();//获取上传文件的名称
                    // 如果上传的文件名称为空，即没有指定上传文件
                    if(name == null || name.isEmpty()) {
                        continue;
                    }
                    switch (fileItem.getFieldName()){
                        case "RWS":RWS = name;
                            gradeDesign.setRWS(RWS);
                            break;
                        case "KTBG":KTBG = name;
                            gradeDesign.setKTBG(KTBG);
                            break;
                        case "BYLW":BYLW = name;
                            gradeDesign.setBYLW(BYLW);
                            break;
                        case "CODE":CODE = name;
                            gradeDesign.setCODE(CODE);
                            break;
                        default:break;
                    }
                    // 获取真实路径，对应${项目目录}/uploads，当然，这个目录必须存在
                    String savepath = this.getServletContext().getRealPath("/uploads");
                    // 通过uploads目录和文件名称来创建File对象
                    File file = new File(savepath, name);
                    // 把上传文件保存到指定位置
                    fileItem.write(file);

                }
            }
        } catch (Exception e) {
            throw new ServletException(e);
        }
        if(StudentSql.isDesign(ID)){
            boolean a = StudentSql.updataDesign(ID,Depart,Major,Phone,RWS,KTBG,BYLW,CODE);
            if (a){
                response.setHeader("refresh", "1;URL=UploadPaper.jsp");
            }else {
                System.out.println("提交失败");
                response.setHeader("refresh", "1;URL=UploadPaper.jsp");
            }
        }else {
            boolean a = StudentSql.inseDesign(ID,Name,Depart,Major,Title,Phone,RWS,KTBG,BYLW,CODE);
            if (a){
                response.setHeader("refresh", "1;URL=UploadPaper.jsp");
            }else {
                System.out.println("提交失败");
                response.setHeader("refresh", "1;URL=UploadPaper.jsp");
            }
        }
    }

}
